package edu.umn.contactviewer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * This class represents both an Add Activity and an Edit Activity in the same
 * class due to the fact that they use very similar user interface elements.
 */
public class AddEditContactActivity extends Activity {

	Contact currentContact;
	boolean isAdd;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.add_edit_contact);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			currentContact = (Contact) extras
					.getSerializable("selected-contact");
		}

		isAdd = extras.getBoolean("isAdd");

		if (isAdd) {
			new ToolbarConfig(this, "Add Contact");
			((EditText) this.findViewById(R.id.editName)).setEnabled(true);

		} else {
			new ToolbarConfig(this, "Edit: " + currentContact.getName());
			((EditText) this.findViewById(R.id.editName)).setEnabled(false);
			((EditText) this.findViewById(R.id.editTitle)).requestFocus();
		}

		((EditText) this.findViewById(R.id.editName)).setText(currentContact
				.getName());
		((EditText) this.findViewById(R.id.editTitle)).setText(currentContact
				.getTitle());
		((EditText) this.findViewById(R.id.editPhoneNumber))
				.setText(currentContact.getPhone());
		((EditText) this.findViewById(R.id.editEmail)).setText(currentContact
				.getEmail());
		((EditText) this.findViewById(R.id.editTwitter)).setText(currentContact
				.getTwitterId());

		((Button) this.findViewById(R.id.saveButton))
				.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {

						String name = ((EditText) AddEditContactActivity.this
								.findViewById(R.id.editName)).getText()
								.toString();
						String title = ((EditText) AddEditContactActivity.this
								.findViewById(R.id.editTitle)).getText()
								.toString();
						String phoneNumber = ((EditText) AddEditContactActivity.this
								.findViewById(R.id.editPhoneNumber)).getText()
								.toString();
						String email = ((EditText) AddEditContactActivity.this
								.findViewById(R.id.editEmail)).getText()
								.toString();
						String twitter = ((EditText) AddEditContactActivity.this
								.findViewById(R.id.editTwitter)).getText()
								.toString();

						currentContact.setName(name);
						currentContact.setTitle(title);
						currentContact.setEmail(email);
						currentContact.setPhone(phoneNumber);
						currentContact.setTwitterId(twitter);

						Intent intent = new Intent();
						intent.putExtra("selected-contact", currentContact);

						AddEditContactActivity.this.setResult(
								ContactSelectedActivity.ADD_EDIT_CONTACT,
								intent);

						if (isAdd) {
							boolean isValid = ContactsDB.put(currentContact,
									AddEditContactActivity.this);

							if (isValid) {
								Toast.makeText(
										AddEditContactActivity.this,
										currentContact.getName()
												+ " has been saved.",
										Toast.LENGTH_LONG).show();
								AddEditContactActivity.this.finish();
							} else {
								Toast.makeText(
										AddEditContactActivity.this,
										currentContact.getName()
												+ " already exists.  Please try again.",
										Toast.LENGTH_LONG).show();
							}
						} else {

							ContactsDB.update(currentContact,
									AddEditContactActivity.this);

							Toast.makeText(
									AddEditContactActivity.this,
									currentContact.getName()
											+ " has been edited.",
									Toast.LENGTH_LONG).show();
							AddEditContactActivity.this.finish();
						}

					}
				});

		((Button) this.findViewById(R.id.cancelButton))
				.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						Intent intent = new Intent();
						intent.putExtra("selected-contact", currentContact);

						AddEditContactActivity.this.setResult(
								ContactSelectedActivity.ADD_EDIT_CONTACT,
								intent);

						AddEditContactActivity.this.finish();

					}
				});
	}
}
